<template>
  <div class="container">
    <header-search-form
      :searchFormData="searchForm"
      @search="search"
    />
    <lan-table
      size="small"
      :config="dataTable"
      :tableData="tableData"
      @search="search"
    >
      <template #type="scope">
        <el-input v-model="scope.row.type"></el-input>
      </template>
      <template #active="scope">
        <span
          v-if="scope.row.active"
          style="color:#07c160"
        >允许</span>
        <span
          v-else
          style="color:#fa5151"
        >不允许</span>
      </template>
    </lan-table>
    <up-load
      ref="uploadComponent"
      @refresh="search"
      @closeExpand="closeExpand"
    ></up-load>
    <edit
      ref="editComponent"
      @refresh="search"
    ></edit>
  </div>
</template>

<script>
import lanTable from '../../../components/lanTable';
import UpLoad from './upload';
import Edit from './edit';
import HeaderSearchForm from './headerSearchForm.vue';
import * as moment from 'moment';
import { SortType, MomentFormat } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { parseError } from '../../../utils';
import { getPackageListApi, deletePackageApi } from '../../../api/package';

export default {
  components: {
    lanTable,
    UpLoad,
    Edit,
    HeaderSearchForm
  },
  data() {
    return {
      dataTable: {
        pageSize: 10,
        totalCount: 0,
        currentPage: 1,
        pageSizes: [5, 10, 20, 50, 100],
        sort: {
          orderBy: 'createdAt',
          sortType: SortType.DESC
        },
        columnList: [
          {
            name: 'index',
            label: '',
            type: 'index'
          },
          {
            name: 'name',
            label: '名称'
          },
          {
            name: 'versionName',
            label: '版本号'
          },
          {
            name: 'deviceTypeName',
            label: '产品类型'
          },
          {
            name: 'packageName',
            label: '文件名称'
          },
          {
            name: 'active',
            label: '允许升级',
            type: 'slot'
          },
          {
            name: 'size',
            label: '字节数'
          },
          {
            label: '产品列表',
            type: 'button',
            scope: true,
            buttons: [
              {
                size: 'small',
                text: '查看',
                icon: 'el-icon-tickets',
                method: row => this.toProductList(row.name, row.id)
              }
            ]
          },
          {
            name: 'description',
            label: '备注'
          },
          {
            label: '产品导入',
            type: 'button',
            scope: true,
            buttons: [
              {
                size: 'small',
                text: '上传文件',
                icon: 'el-icon-document-copy',
                method: row => this.showUpload(row.id)
              }
            ]
          },
          {
            name: 'createdAt',
            label: '创建时间',
            sortable: true
          },
          {
            name: 'updatedAt',
            label: '更新时间'
          },
          {
            type: 'button',
            label: '操作',
            buttons: [
              {
                type: 'text',
                text: '编辑',
                size: 'small',
                method: row => {
                  this.showEdit(row);
                }
              },
              {
                type: 'text',
                text: '删除',
                size: 'small',
                method: row => {
                  this.remove(row.uuid, row.name);
                }
              }
            ]
          }
        ]
      },
      tableData: [],
      searchForm: {
        btnLoading: false,
        deviceTypeUuid: '',
        name: '',
        description: ''
      }
    };
  },
  created() {
    this.search();
  },
  methods: {
    search() {
      this.searchForm.btnLoading = true;
      getPackageListApi(
        this.dataTable.currentPage - 1,
        this.dataTable.pageSize,
        this.dataTable.sort.sortType,
        this.searchForm.deviceTypeUuid,
        this.searchForm.name,
        this.searchForm.description,
        this.searchForm.active
      )
        .then(({ total_count, data }) => {
          this.dataTable.totalCount = total_count;
          this.tableData = data.map(upgradePackage => {
            return {
              id: upgradePackage.id,
              name: upgradePackage.name,
              description: upgradePackage.description,
              versionName: upgradePackage.version_name,
              packageUrl: upgradePackage.package_url,
              size: upgradePackage.size,
              deviceTypeUuid: upgradePackage.product_type_uuid,
              deviceTypeName: upgradePackage.product_type_name,
              createdAt: moment(upgradePackage.created_at).format(MomentFormat),
              updatedAt: moment(upgradePackage.updated_at).format(MomentFormat),
              packageName: upgradePackage.package_url.split('/')[upgradePackage.package_url.split('/').length - 1],
              active: upgradePackage.active
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    toProductList(name, packageId) {
      let productListRoute = this.$router.resolve({
        name: 'upgradeDevice',
        query: {
          packageName: name,
          packageId: packageId
        }
      });
      window.open(productListRoute.href, '_blank');
    },
    showUpload(packageId) {
      this.packageId = packageId;
      this.$refs.uploadComponent.showDialog(packageId);
    },
    showEdit(row) {
      this.$refs.editComponent.showDialog(row);
    },
    remove(id, name) {
      MessageBox.confirm(`确认删除${name}吗？`, '删除提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deletePackageApi(id)
          .then(() => Message.success('删除升级包成功'))
          .catch(error => Message.error(parseError(error)))
          .finally(() => this.search());
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 10px;
}
</style>
